package com.example.tabbar.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.R
import com.example.tabbar.room.bean.NavItem
import com.example.tabbar.databinding.NavItemVerticalBinding

class NavigationAdapter : RecyclerView.Adapter<NavigationAdapter.ViewHolder>() {
    private var items: List<NavItem>? = null
    private var selectedPosition = 0
    private var onItemClickListener: ((Int) -> Unit)? = null
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding =
            NavItemVerticalBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items!![position]
        holder.icon.setImageResource(item.icons)
        holder.title.text = item.title

        // 设置选中状态背景
        if (position == selectedPosition) {
            holder.itemView.setBackgroundResource(
                R.drawable.nav_vertical_selected
            )
            holder.icon.setColorFilter(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.nav_selected_color
                )
            ) // 替换为你的选中颜色资源
            holder.title.setTextColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.nav_selected_color
                )
            ) // 替换为你的选中颜色资源

        } else {
            // 未选中状态的背景 (你需要创建 default_background drawable 或使用透明背景)
            holder.itemView.setBackgroundResource(R.drawable.nav_vertical_unselected)
            holder.icon.setColorFilter(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.nav_unselected_color
                )
            ) // 替换为你的选中颜色资源
            holder.title.setTextColor(
                ContextCompat.getColor(
                    holder.itemView.context,
                    R.color.nav_unselected_color
                )
            )

        }

        // 设置点击事件
        holder.itemView.setOnClickListener {
            val currentPosition = holder.adapterPosition
            if (currentPosition != RecyclerView.NO_POSITION) {
                // 更新选中状态
                val previousPosition = selectedPosition
                selectedPosition = currentPosition

                // 通知数据改变
                notifyItemChanged(previousPosition)
                notifyItemChanged(selectedPosition)

                onItemClickListener?.invoke(currentPosition)
            }
        }
    }

    override fun getItemCount(): Int {
        return items?.size ?: 0
    }

    fun setItems(items: List<NavItem>?) {
        this.items = items
        notifyDataSetChanged()
    }


    class ViewHolder(binding: NavItemVerticalBinding) : RecyclerView.ViewHolder(binding.root) {
        val icon = binding.icon
        val title = binding.title
    }

    // 设置点击监听器
    fun setOnItemClickListener(listener: (Int) -> Unit) {
        this.onItemClickListener = listener
    }

    // 获取当前选中位置
    fun getSelectedPosition(): Int {
        return selectedPosition
    }

    // 设置选中位置
    fun setSelectedPosition(position: Int) {
        if (position >= 0 && position < itemCount) {
            val previousPosition = selectedPosition
            selectedPosition = position

            notifyItemChanged(previousPosition)
            notifyItemChanged(selectedPosition)
        }
    }
}